package com.sample.service.scheduling;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.SimpleTriggerBean;

import java.util.Date;
import java.util.List;

/**
 * User: khanhpham
 * Date: 10/26/11
 */
public class SchedulerFactory {
   @Autowired
   private Scheduler scheduler;

   public void scheduleJobs(List<CronTriggerBean> cronTriggerBeans) {
      for (CronTriggerBean cronTriggerBean : cronTriggerBeans) {
         JobDetail jobDetail = cronTriggerBean.getJobDetail();
         try {
            scheduler.scheduleJob(jobDetail, cronTriggerBean);
         } catch (SchedulerException e) {
            e.printStackTrace();
         }
      }
   }

   public void scheduleJob(CronTriggerBean cronTriggerBean) {
      JobDetail jobDetail = cronTriggerBean.getJobDetail();
      try {
         scheduler.scheduleJob(jobDetail, cronTriggerBean);
      } catch (SchedulerException e) {
         e.printStackTrace();
      }
   }

   public void scheduleJob(SimpleTriggerBean simpleTriggerBean) {
      JobDetail jobDetail = simpleTriggerBean.getJobDetail();
      try {
         scheduler.scheduleJob(jobDetail, simpleTriggerBean);
      } catch (SchedulerException e) {
         e.printStackTrace();
      }
   }
}
